/*
 * TestUtil.java
 *
 * Created on Jun 3, 2007, 9:58:23 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.infomancers.workflow.tests;

import com.infomancers.collections.Action;
import com.infomancers.collections.Predicate;
import com.infomancers.collections.actions.EmptyAction;
import com.infomancers.collections.predicates.ConstantPredicate;
import com.infomancers.workflow.FlowManager;
import com.infomancers.workflow.Place;
import com.infomancers.workflow.Token;
import com.infomancers.workflow.Transition;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author aviadbd
 */
public class TestUtil {
    
    public static void updateTransitionsAccordingToPlaces(FlowManager flowManager,Place... places) {
        Map<Transition,Set<Place>> transitions = new HashMap<Transition,Set<Place>>();
        
        for (Place p : places) {
            for (Transition t : p.getOutputTransitions()) {
                Set<Place> tp = transitions.get(t);
                if (tp == null) {
                    tp = new HashSet<Place>();
                    transitions.put(t, tp);
                }
                
                tp.add(p);
            }
        }
        
        for (Map.Entry<Transition,Set<Place>> entry : transitions.entrySet()) {
            entry.getKey().setFlowManager(flowManager);
            entry.getKey().setInputPlaces(entry.getValue());
        }
    }
    
    public static void setEmptyPredicates(Transition... transitions) {
        for (Transition t : transitions) {
            t.setPredicate(new ConstantPredicate<Token>(true));
        }
    }
    
    public static void setEmptyActions(Transition... transitions) {
        for (Transition t : transitions) {
            t.setAction(new EmptyAction<Object>());
        }
    }
}
